import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
 * @program: LeetCode
 * @ClassName Num032_1_levelOrder
 * @description: LeetCode : 剑指 Offer 32 - I. 从上到下打印二叉树
 * @author: WXY
 * @create: 2022-12-09 13:59
 * @Version 1.0
 **/


public class Num032_1_levelOrder {
    public class TreeNode {
      int val;
      TreeNode left;
      TreeNode right;
      TreeNode(int x) { val = x; }
  }
    public static int[] levelOrder(TreeNode root) {
      if (root == null) return new int[0];
      LinkedList<TreeNode> queue = new LinkedList<>();
      List<Integer> list = new ArrayList<>();
      int[] ans = new int[1024];
      int index = 0;
      queue.add(root);
      while (!queue.isEmpty()) {
        TreeNode node = queue.poll();
//        list.add(node.val);
        ans[index++] = node.val;
        if (node.left!= null) queue.add(node.left);
        if (node.right != null)queue.add(node.right);
      }
//      int[] ans = new int[list.size()];
//      for (int i = 0; i < ans.length; i++) {
//        ans[i] = list.get(i);
//      }
      return ans;
    }
}
